package com.springboot.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.springboot.demo.entity.BorrowMsg;
import com.springboot.demo.entity.Reader;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ReaderMapper extends BaseMapper<Reader> {
    @Select("select * from reader where phone=#{phone} and password=#{password}")
    Reader findreader(String phone, String password);

    @Select("select * from reader where phone=#{phone}")
    Reader findByPhone(String phone);

    @Update("update reader set rname = #{name},password=#{password} where rname = #{orname} and password = #{opassword}")
    void updateInfoReader(@Param("orname") String oname, @Param("opassword") String opassword, @Param("name") String name, @Param("password") String password);

    @Update("update borrow set rname = #{name} where rname = #{orname}")
    void updateInfoBorrow(@Param("orname") String oname, @Param("opassword") String opassword, @Param("name") String name, @Param("password") String password);

    @Update("update readerfinerecord set rname = #{name} where rname = #{orname}")
    void updateInfoReaderReaderFineRecord(@Param("orname") String oname, @Param("opassword") String opassword, @Param("name") String name, @Param("password") String password);

    @Update("update reserve set rname = #{name} where rname = #{orname}")
    void updateInfoReaderReserve(@Param("orname") String oname, @Param("opassword") String opassword, @Param("name") String name, @Param("password") String password);


    @Select("select * from reader where rname=#{name} limit 1")
    Reader findByName(@Param("name") String username);

    @Select("select * from reader where email=#{email} limit 1")
    Reader getPassword(@Param("email") String email);

    @Select("select * from borrow where phone=#{phone} and state = 'borrowing'")
    List<BorrowMsg> getBooks(@Param("phone") String phone);

    @Update("update readerFineRecord set ispaid = 'paid' where phone = #{phone}" )
    void payReaderFine(@Param("phone")String phone);

    @Update("update reader set deposit = deposit - #{totalFine} where phone = #{phone}")
    void decFine(@Param("phone")String phone, @Param("totalFine")int totalFine);

    //读者注册
    @Insert("insert into reader set phone=#{phone},rname=#{rname},password=#{password},email=#{email},date=#{date},number=#{number},deposit=#{deposit},register=#{register}")
    void addReader(String phone,String rname,String password,String email, String date, Integer number, Integer deposit, String register);

    //支付保证金
    @Update("update reader set deposit=#{deposit} where phone=#{phone}")
    void payDeposit(String phone,int deposit);

    //更改电话和姓名
    @Update("update reader set name=#{name},set password = #{password} where(phone=#{phone})")
    void modify_NP(String phone,String name,String password);

    //更改手机
    @Update("update reader set phone=#{phone} where phone=#{ophone} ")
    void modify_Phone(String ophone,String phone);

    //删除
    @Delete("delete from reader where phone=#{phone}")
    void deleteReader(String phone);
    //获得所有已通过申请的读者表单
    @Select("select * from reader where register = 'yes'")
    List<Reader> getall();

    @Update("update finerecord set state='unpaid' where phone=#{phone} and state='sending'" )
    void updatefine(String phone);
}

